function D = calculateDiagonalPreconditioner3D(H,n,precision)
    e0=zeros(n,n,n,precision);
    D=zeros(n^3,1,precision);
    L=3*n-2;
    Hhat=fftn(H.filter,[L,L,L]);
    X0=zeros(L,L,L,precision);
    for i = 1:n^3
        e1=e0;
        e1(i)=1;
        %B=optimizedAdjointPPFT3D(optimizedPPFT3D(e1,precision),precision);
        Xhat=X0;
        Xhat(1:n,1:n,1:n)=e1;
        Xhat=fftn(Xhat);
        Xhat=ifftn(Xhat.*Hhat);
        e1=Xhat(n:2*n-1,n:2*n-1,n:2*n-1);
        D(i)=1/e1(i);
    end
    filename=sprintf('preconditioners%sDiagonal3D%dprecision%s.dat',filesep,n,precision);
    fid=fopen(filename,'w');
    if fid<0
        error('failed to open file %s for writing, filter was not created\n',filename);
    else
        if strcmp(precision,'single')
            fwrite(fid,D,'real*4');
        elseif strcmp(precision,'double')
            fwrite(fid,D,'real*8');
        else
            error('precision must be single or double');
        end
        fclose(fid);
    end
end
